<?php 

Class articleModel extends AppModel
{
	
	
	public function ordering($val,$id)
	{
		$result=array('error'=>0,'content'=>'');
		$sql="UPDATE ".$this->table('article')." SET ordering='$val' WHERE id='".$id."'";
		$res=$this->query($sql);
		if(!$res)
		{
			$result['error']=1;
			$result['content']='';
		}
		return $result;
	}
	public function get_html_art($id)
	{
		$sql="SELECT a.id,m.code,FROM_UNIXTIME(a.created,'%Y%m%d') as addtime FROM ".$this->table('category')." as m,".$this->table('article')." as a WHERE a.cat_id=m.id AND a.id='".$id."'";
		$art=$this->getRow($sql);
		return $art;
	}
	function del_art($id)
	{
		$result=array('error'=>0,'content'=>'');
		if($id)
		{
		
				$sql="DELETE FROM ".$this->table('article')." WHERE  id='".$id."'";
		
				$res=$this->query($sql);
				 
				if(!$res)
				{
					$result['error']=1;
					$result['content']='删除失败';
				}
	
	
		}
			 return $result;
	
	
	}
	function get_page_art($cat_id)
	{
		$sql="SELECT id FROM ".$this->table('article')." WHERE cat_id='".$cat_id."'";
		return $this->getOne($sql);
	}
	function del_photo($id)
	{
		$result=array('error'=>0,'content'=>'删除成功');
		if($id)
		{
	
			$sql="DELETE FROM ".$this->table('album_photos')." WHERE  id='".$id."'";
	
			$res=$this->query($sql);
	
			if(!$res)
			{
				$result['error']=1;
				$result['content']='删除失败';
			}
	
	
		}
		return $result;
	
	
	}
	public function get_art($id)
	{
		if($id)
		{
		  $sql="SELECT a.*,c.name as cat_name FROM ".$this->table('article')." as a LEFT JOIN ".$this->table('category')." as c ON a.cat_id=c.id WHERE a.id = '".$id."'";
		  $result=$this->getRow($sql);
		  if($result)
		  {
		  	$result['cat_name']=hy_dig_filter($result['cat_name'],1);
		  	$arr=hy_dig_filter($result['title'],3);
		  	$arr2=hy_dig_filter($result['author'],3);
		  	$arr3=hy_dig_filter($result['content'],3);
		  	$arr4=hy_dig_filter($result['summary'],3);
		  	$result['en_content']=stripslashes($arr3[1]);
		  	$result['content']=stripslashes($arr3[0]);
		  	$result['en_author']=$arr2[1];
		  	$result['author']=$arr2[0];
		  	$result['en_title']=$arr[1];
		  	$result['title']=$arr[0];
		  	$result['en_summary']=$arr4[1];
		  	$result['summary']=$arr4[0];
		  	
		  	$result['flags']=explode(',', $result['flags']);
		  	
		  	//获取相册集
		  	$sql="SELECT p.* FROM ".$this->table('album_photos')." as p WHERE p.aid = '".$id."'";
		  		
		  	$photo=$this->getAll($sql);
		  	foreach($photo as $k=>$v)
		  	{
		  		$arr=hy_dig_filter($v['title'],3);
		  		$photo[$k]['title']=$arr[0];
		  	}
		  	$result['photo']=$photo;
			return $result;
		  }
		  else
		  {
			return $result=array();
		  }
		}
	}
	public function update_art($info,$photo,$id)
	{
		if($id)
		{
			foreach($info as $k=>$v)
			{
					$where.=" $k='$v',";
			}
			
			$where.="modified='".$_SERVER['REQUEST_TIME']."'";
		
			$sql="UPDATE ".$this->table('article')." SET ".$where." WHERE id='".$id."'";
			
			$res=$this->query($sql);
			
			$aid=$id;
			if($photo)
			{
				$sql="INSERT INTO ".$this->table('album_photos')." (aid,original_pic) VALUES";
				if(is_array($photo))
				{
				    foreach($photo as $k=>$v)
				  {
					$sql.="('$aid','$v'),";
				  }
				}
				else 
				{
					$sql.="('$aid','$photo'),";
				}
				$sql = substr($sql,0,strlen($sql)-1);
				$this->query($sql);
			}
			
			return $res;
		}
	}
	
	public function create_art($info,$photo)
	{
		$field='';
		$value='';
		$result=array('error'=>0,'content'=>'');
			foreach($info as $k=>$v)
			{
				if($v!='')
				{
					$field.="$k,";
						
						
					$value.="'$v',";
			
				}
			}
			$field=substr($field,0,strlen($field)-1);
			$value=substr($value,0,strlen($value)-1);
			$sql="INSERT INTO ".$this->table('article')." (".$field.") VALUES(".$value.")";
			if(!$res=$this->query($sql))
			{
				$result['error']=1;
				$result['content']=Lang('SQL_ERROR');
			}
			else 
			{
				$aid=$this->insert_id();
				
				if(count($photo)>0)
				{
					
					$sql="INSERT INTO ".$this->table('album_photos')." (aid,original_pic) VALUES";
					foreach($photo as $k=>$v)
					{
					    $sql.="('$aid','$v'),";
					}
					$sql = substr($sql,0,strlen($sql)-1);
					$this->query($sql);
				}
			}
		
	
		return $result;
		//$this->make_thumb('/Uploads/ad/1398673595657549402.jpg','200','200');
	}
	public function get_all_art($sort=array())
	{
		$where='1=1';
		$sql="SELECT a.id,a.title,a.ordering,c.name FROM (".$this->table('article')." as a) LEFT JOIN ".$this->table('category')." as c ON a.cat_id=c.id WHERE ".$where." ORDER BY a.id DESC";
		$result= $this->getAll($sql);
		foreach($result as $k=>$v)
		{
			$result[$k]['title']=hy_dig_filter($v['title'],1);
			$result[$k]['name']=hy_dig_filter($v['name'],1);
			$result[$k]['url']='http://'.$_SERVER['HTTP_HOST'].'/index.php?g=home&m=article&a=view&id='.$v['id'];
				
		}
		return $result;
	}
	public function art_list($page=1,$sort=array())
	{
		$listPage=array('data'=>'','pageLink'=>'');
		$where=' 1=1';
		if($sort['cat_id']>0)
		{
			$where.=' AND cat_id='.$sort['cat_id'];
		}
		elseif($sort['cat_id']==-1)//未分类 
		{
			$where.=' AND c.name is null';
		}
		if($sort['keyword']!='')
		{
			$where.=" AND title like '%".$sort['keyword']."%'";
		}
	$sql="SELECT a.id,a.title,a.ordering,c.name,a.created FROM (".$this->table('article')." as a) LEFT JOIN ".$this->table('category')." as c ON a.cat_id=c.id WHERE ".$where." ORDER BY a.id DESC";
	//$sql="SELECT a.id,a.title,a.ordering,c.name,a.created FROM (".$this->table('article')." as a) , ".$this->table('category')." as c WHERE a.cat_id<>c.id  ORDER BY a.id DESC";
	$result=$this->show_page($sql,$page);
		
		if($result)
		{
		foreach($result['result'] as  $k=>$v)
		{
			$result['result'][$k]['title']=hy_dig_filter($v['title'],1);
			$result['result'][$k]['name']=hy_dig_filter($v['name'],1);
			
		} 
		}
		$listPage['pagelink']=$result['page'];
	    $listPage['data']=$result['result'];
		
		
		return $listPage;
	}
	public function update_photo($info,$id)
	{
		if($id)
		{
			$sql="SELECT original_pic FROM ".$this->table('album_photos')." WHERE id=".$id;
			$old_pic=$this->getOne($sql);
			if($old_pic!=$info['original_pic '])
			{
				if(is_file(ROOT_PATH."/".$info['original_pic']))
				{
					$info['middle_pic']=$this->make_thumb('/'.$info['original_pic'],'200','200');
					$info['small_pic']=$this->make_thumb('/'.$info['original_pic'],'80','80');
				}
			}
			foreach($info as $k=>$v)
			{
	
				$where.=" $k='$v',";
	
	
			}
	
			$where.="modified='".$_SERVER['REQUEST_TIME']."'";
	
			$sql="UPDATE ".$this->table('album_photos')." SET ".$where." WHERE id='".$id."'";
	
			return $res=$this->query($sql);
		}
	}
	public function get_photo($id)
	{
		$sql="SELECT p.* FROM ".$this->table('album_photos')." as p  WHERE p.id = '".$id."'";
			
		$result=$this->getRow($sql);
		if($result)
		{
			$arr=hy_dig_filter($result['title'],3);
			$arr2=hy_dig_filter($result['abstract'],3);
				
		
			$result['title']=$arr[0];
			$result['en_title']=$arr[1];
			$result['abstract']=$arr2[0];
			$result['en_abstract']=$arr2[1];
			return $result;
		}
		else
		{
			return $result=array();
		}
	}
	
	public function photo_list($id)
	{
	
		$sql="SELECT aid FROM (".$this->table('album_photos')." as p)  WHERE id='".$id."'";
		$aid=$this->getOne($sql);
		
		$sql="SELECT p.* FROM ".$this->table('album_photos')." as p WHERE p.aid = '".$aid."'";
		
		$photo=$this->getAll($sql);
		foreach($photo as $k=>$v)
		{
			$arr=hy_dig_filter($v['title'],3);
			$photo[$k]['title']=$arr[0];
			$photo[$k]['en_title']=$arr[1];
			
			$arr=hy_dig_filter($v['abstract'],3);
			$photo[$k]['abstract']=$arr[0];
			$photo[$k]['en_abstract']=$arr[1];
		}
		
		return $photo;
	}
	
	
}